/**
 * 
 */

package com.pp.admin.login.client;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.pp.admin.common.client.beans.LoginUserInfo;
import com.pp.admin.common.client.exceptions.DomainAuthException;
import com.pp.admin.common.client.helper.JavaScriptCommon;
import com.pp.admin.common.client.service.Service;

/**
 * @author pandurang
 */
public class LoginPage extends BasePage implements Service {
	
	private LayoutPanel			contents		= new LayoutPanel();
	
	private VerticalPanel		loginTile		= new VerticalPanel();
	
	private TextBox				userID			= new TextBox();
	
	private PasswordTextBox		password		= new PasswordTextBox();
	
	private HTML				errorMessage	= new HTML();
	
	private Button				login			= new Button( myConstants.login() );
	
	private static final int	USERNAME_ROW	= 0;
	
	private static final int	PASSWORD_ROW	= 1;
	
	/**
	 * @param unit
	 */
	public LoginPage( Unit unit, Login parent ) {

		super( unit, parent );
		HTML help = new HTML( "Help contents" );
		HTML loginMessage = new HTML( msgs.loginmsg( parent.getDomain().getUesrFriendlyName() ) );
		loginTile.add( loginMessage );
		loginTile.setCellHorizontalAlignment( loginMessage, HasHorizontalAlignment.ALIGN_CENTER );
		loginTile.add( errorMessage );
		loginTile.setCellHorizontalAlignment( errorMessage, HasHorizontalAlignment.ALIGN_CENTER );
		FlexTable loginInput = new FlexTable();
		loginInput.setWidget( USERNAME_ROW, LABEL_COL, new Label( myConstants.username() + " : " ) );
		loginInput.setWidget( USERNAME_ROW, FLD_COL, userID );
		loginInput.getCellFormatter().setHorizontalAlignment( USERNAME_ROW, LABEL_COL, HasHorizontalAlignment.ALIGN_RIGHT );
		loginInput.setWidget( PASSWORD_ROW, LABEL_COL, new Label( myConstants.pwd() + " : " ) );
		loginInput.setWidget( PASSWORD_ROW, FLD_COL, password );
		loginInput.getCellFormatter().setHorizontalAlignment( PASSWORD_ROW, LABEL_COL, HasHorizontalAlignment.ALIGN_RIGHT );
		login.addClickHandler( new ClickHandler() {
			
			@Override
			public void onClick( ClickEvent event ) {

				loginService.login( userID.getText(), password.getText(), LoginPage.this.parent.getDomain().getDomainID(), new AsyncCallback<LoginUserInfo>() {
					
					@Override
					public void onSuccess( LoginUserInfo result ) {

						JavaScriptCommon.redirectToUrl( LoginPage.this.parent.getDomainRedirectURL() );
					}
					
					@Override
					public void onFailure( Throwable caught ) {

						password.setText( "" );
						password.setFocus( true );
						if ( caught instanceof DomainAuthException ) {
							errorMessage.setHTML( "<font color='red'>" + msgs.invalidDomainAuth() + "</font>" );
						} else {
							errorMessage.setHTML( "<font color='red'>" + msgs.invalidUserCreds() + "</font>" );
						}
						
					}
				} );
				
			}
		} );
		loginInput.setWidget( 3, 1, login );
		loginTile.add( loginInput );
		contents.add( help );
		contents.add( loginTile );
		contents.setWidgetLeftWidth( help, 0, Unit.PCT, 50, Unit.PCT ); // Left panel
		contents.setWidgetRightWidth( loginTile, 0, Unit.PCT, 30, Unit.PCT ); // Right panel
		add( contents );
	}
	
	public void setFocus() {

		userID.setFocus( true );
		
	}
}
